﻿// 以下代码是配置layui扩展模块的目录，每个页面都需要引入
layui.config({
    version: '410',
    base: getProjectUrl() + 'assets/module/'
}).extend({
    formSelects: 'formSelects/formSelects-v4',
    treetable: 'treetable-lay/treetable',
    dropdown: 'dropdown/dropdown',
    notice: 'notice/notice',
    step: 'step-lay/step',
    dtree: 'dtree/dtree',
    citypicker: 'city-picker/city-picker',
    tableSelect: 'tableSelect/tableSelect',
    Cropper: 'Cropper/Cropper',
    zTree: 'zTree/zTree',
    introJs: 'introJs/introJs',
    fileChoose: 'fileChoose/fileChoose',
    tagsInput: 'tagsInput/tagsInput',
    Drag: 'Drag/Drag',
    CKEDITOR: 'ckeditor/ckeditor',
    Split: 'Split/Split',
    cascader: 'cascader/cascader',
    treeSelect: 'treeSelect/treeSelect',
    steps: 'steps/steps',
    xmSelect: 'Xmselect/xmselect'
}).use(['layer', 'admin'], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var admin = layui.admin;

    // 移除loading动画
    setTimeout(function () {
        admin.removeLoading();
    }, window == top ? 600 : 100);


    $.ajaxSetup({
        error: function (xhr) {
            var msg = xhr.responseText ? xhr.responseText : "API接口异常";
            if (xhr.status == 401) {
                var res = JSON.parse(msg);
                layer.msg(res.message, { icon: 5, time: 2000 }, function () {
                    location.replace('/manage/login');
                });
            }
            else if (xhr.status == 500) {
                var res = JSON.parse(msg);
                layer.msg(res.message, { icon: 5, time: 2000 });
            }
            else {
                var res = JSON.parse(msg);
                layer.msg(res.message, { icon: 5, time: 2000 });
            }
            layer.closeAll('loading');
        }
    });
});

// 获取当前项目的根路径，通过获取layui.js全路径截取assets之前的地址
function getProjectUrl() {
    var layuiDir = layui.cache.dir;
    if (!layuiDir) {
        var js = document.scripts, last = js.length - 1, src;
        for (var i = last; i > 0; i--) {
            if (js[i].readyState === 'interactive') {
                src = js[i].src;
                break;
            }
        }
        var jsPath = src || js[last].src;
        layuiDir = jsPath.substring(0, jsPath.lastIndexOf('/') + 1);
    }
    return layuiDir.substring(0, layuiDir.indexOf('assets'));
}



// 计算文件大小
function bytesToSize(bytes) {
    if (bytes === 0) return '0 B';
    var k = 1024;
    sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];

    i = Math.floor(Math.log(bytes) / Math.log(k));
    var num = bytes / Math.pow(k, i);
    return num.toPrecision(3) + ' ' + sizes[i];
}

//增加千位符
function numFormat(s,n=2) {
    n = n > 0 && n <= 20 ? n : 2;
    s = parseFloat((s + '').replace(/[^\d\.-]/g, '')).toFixed(n) + '';
    var l = s.split('.')[0].split('').reverse(),
        r = s.split('.')[1];
    var t = '';
    for (var i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? ',' : '');
    }
    return t.split('').reverse().join('') + '.' + r;
}